package org.luxor.common.netty.stream.client;


import io.netty.channel.Channel;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.luxor.common.netty.TcpNioClient;
import org.luxor.common.netty.config.ClientPipelineConfig;
import org.luxor.common.netty.config.properties.NettyClientProperties;
import org.luxor.common.netty.stream.client.handler.WriteStreamHandler;

/**
 * Netty简单案例：非阻塞I/O案例 客户端
 *
 * @author Mr.yan @date 2023/8/29
 */
public class StreamClient {

    public static void main(String[] args) throws Exception {
        int port = 8080;
        if (args != null && args.length > 0) {
            port = Integer.parseInt(args[0]);
        }
        NettyClientProperties clientProperties = new NettyClientProperties("client-001", "127.0.0.1", port);
        new TcpNioClient(new ClientPipelineConfig() {
            @Override
            public NettyClientProperties clientProperties() {
                return clientProperties;
            }

            @Override
            public void initChannel(Channel channel) throws Exception {
                // 添加 WriteStreamHandler 以实现：连接建立时，自动发送文件数据(ChunkedInput)
                channel.pipeline().addLast(new WriteStreamHandler());
                // 添加 ChunkedWriteHandler 以处理作为 ChunkedInput 传入的数据
                channel.pipeline().addLast(new ChunkedWriteHandler());
            }
        }).start();
    }

}
